package br.com.ecomanda.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import br.com.ecomanda.exceptions.ComandaAbertaMaisDeUmaVezException;
import br.com.ecomanda.exceptions.ConexaoComBancoDeDadosException;
import br.com.ecomanda.model.Comanda;
import br.com.ecomanda.model.OperacaoComanda;

public class OperacaoComandaDao extends GenericDao<OperacaoComanda> {

	public OperacaoComandaDao() throws ConexaoComBancoDeDadosException {
		super();
		// TODO Auto-generated constructor stub
	}

	@SuppressWarnings("unchecked")
	public OperacaoComanda getOperacaoAberta(Comanda comanda)
			throws ComandaAbertaMaisDeUmaVezException {
		Criteria criteria = createCriteria();

		criteria.add(Restrictions.and(Restrictions.eq("comanda", comanda),
				Restrictions.isNull("dtEncerramento")));

		List<OperacaoComanda> operacoes = criteria.list();

		if (operacoes == null)
			return null;
		else if (operacoes.size() == 0)
			return null;
		else if (operacoes.size() > 1)
			throw new ComandaAbertaMaisDeUmaVezException();

		return operacoes.get(0);
	}

}
